/**
 * 
 */
package kr.co.insoft.publicmesh.controller.template.application;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import kr.co.insoft.core.model.template.application.ApplicationEntityModel;
import kr.co.insoft.core.model.template.application.ParamEntityModel;
import kr.co.insoft.core.service.template.application.ApplicationService;
import kr.co.insoft.core.service.template.application.ParamService;
import kr.co.insoft.framework.exceptions.ServiceException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author IN-SOFT Inc. Park Jong Taek(jtpark@in-soft.co.kr)
 * 
 */
@Controller
@RequestMapping(value = "template/param")
public class ParamController {
	
	final String TILES_PAGE_PREFIX = "tiles/template/application/param";
	final String SINGLE_PAGE_PREFIX = "template/application/param";

	@Autowired
	ParamService paramService;
	
	@Autowired 
	ApplicationService applicationService;
	
	/**
	 * Param 등록화면
	 * 
	 * @param model, applicationid
	 * @return
	 * @throws ServiceException 
	 */
	@RequestMapping(value = "createForm", method = RequestMethod.GET)
	public String createForm(
			Model model,
			@RequestParam(value = "applicationid", required = true) String applicationid,
			@RequestParam(value = "ip", defaultValue = "Y", required = false) String ip) throws ServiceException {
		
		ApplicationEntityModel application = 
				applicationService.getApplication(applicationid);
		
		ParamEntityModel paramEntityModel = new ParamEntityModel();
		paramEntityModel.setApplicationid(application.getApplicationid());
		
		model.addAttribute("paramEntityModel", paramEntityModel);
	
		if (ip.equals("Y"))
			return SINGLE_PAGE_PREFIX+"/createForm";
		
		return TILES_PAGE_PREFIX + "/createForm";
	}
	
	/**
	 * Param 등록
	 * 
	 * @param paramEntityModel
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "createForm", method = RequestMethod.POST)
	public @ResponseBody ResponseEntity<Boolean> createParam(
			@ModelAttribute("paramEntityModel") ParamEntityModel paramEntityModel,
			final BindingResult bindingResult) throws Exception {
		try {
			if (bindingResult.hasErrors()) {
				return new ResponseEntity<Boolean>(false, HttpStatus.OK);
			}
			paramService.createParam(paramEntityModel);
		
			return new ResponseEntity<Boolean>(true, HttpStatus.OK);
		} catch (ServiceException e) {
			return new ResponseEntity<Boolean>(false, HttpStatus.OK);
		}
	}
	
	/**
	 * Param 상세 화면
	 * 
	 * @param paramid
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "edit", method = RequestMethod.GET)
	public String edit(
			Model model,
			@RequestParam("targetid") String paramid,
			@RequestParam(value = "ip", defaultValue = "Y", required = false) String ip)
			throws Exception {
		model.addAttribute("paramEntityModel", paramService.getParam(paramid));
		if (ip.equals("Y"))
			return SINGLE_PAGE_PREFIX + "/edit";

		return TILES_PAGE_PREFIX + "/edit";
	}
	
	/**
	 * Param 수정
	 * 
	 * @param paramEntityModel
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "edit", method = RequestMethod.POST)
	public @ResponseBody ResponseEntity<Boolean> edit(
			@ModelAttribute("paramEntityModel") ParamEntityModel paramEntityModel) {		
		try {
			paramService.doEditParam(paramEntityModel);
			return new ResponseEntity<Boolean>(true, HttpStatus.OK);
		} catch (ServiceException e) {
			return new ResponseEntity<Boolean>(false, HttpStatus.OK);
		}
	}
	
	/**
	 * Param 삭제
	 * 
	 * @param paramEntityModel
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "remove", method = RequestMethod.POST)
	public @ResponseBody ResponseEntity<Boolean> remove(
			@RequestParam("paramid") String paramid) {			
		try {
			paramService.doRemoveParam(paramid);
			return new ResponseEntity<Boolean>(true, HttpStatus.OK);
		} catch (ServiceException e) {
			return new ResponseEntity<Boolean>(false, HttpStatus.OK);
		}
	}
	
	/**
	 * Param Name 중복 체크
	 * 
	 * @param ParamEntityModel
	 * @return
	 */
	@RequestMapping(value = "isExistParamName", method = RequestMethod.POST)
	public @ResponseBody ResponseEntity<Boolean> isExistParamName(
			@ModelAttribute("paramEntityModel") ParamEntityModel paramEntityModel) {
		try {
			if (paramService.isExistParamName(paramEntityModel))
				return new ResponseEntity<Boolean>(true, HttpStatus.OK);
		} catch (ServiceException e) {
			return new ResponseEntity<Boolean>(false, HttpStatus.OK);
		}
		return new ResponseEntity<Boolean>(false, HttpStatus.OK);
	}
	
	/**
	 * Event 목록
	 * 
	 * @param scriptid
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "list", method = RequestMethod.GET)
	public @ResponseBody  List<ParamEntityModel> list(
			Model model,
			@RequestParam("applicationid") String applicationid)
			throws Exception {

		Map<String, Object> param = new HashMap<String, Object>();
		
		param.put("applicationid", applicationid);
		
		return paramService.getList(param);
	}
	
}
